<?php

namespace Caisse\CaisseBundle\Controller;

use Caisse\CaisseBundle\Entity\Serveur;
use Caisse\CaisseBundle\Form\ServeurType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

class SessionCaisseController extends Controller
{
        public function CloturerAction(){
            $em =$this->getDoctrine()->getManager();
            $serveurs = $em->getRepository('CaisseBundle:Serveur')->findBy(array('etatServeur'=>true));
            return $this->render('CaisseBundle:SessionCaisse:cloturerCaisse.html.twig', array('serveurs'=>$serveurs));
        }
        public  function getCommandeServeurAction(){
            $em =$this->getDoctrine()->getManager();
            $request = $this->getRequest();
            $id = $request->get('id');
            $serveur =$em->getRepository('CaisseBundle:Serveur')->find($id);
            
            $sessionCaisse = $em->getRepository('CaisseBundle:SessionCaisse')->findBy(array('serveur'=>$serveur,'dateFerm'=>null));
            
            return $this->render('CaisseBundle:SessionCaisse:listCommandeSession.html.twig',array('sessionCaisse'=>$sessionCaisse));
            
        }



        public function listServeurAction()
    {
        $em= $this->getDoctrine()->getManager();
        $serveurs = $em->getRepository("CaisseBundle:Serveur")->findAll();
        
        return $this->render('CaisseBundle:Serveur:listServeur.html.twig', array(
            'serveurs' => $serveurs,
        ));
    }
    
    public function ajoutServeurAction()
    {   
        $em = $this->getDoctrine()->getManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        if($id != "")
        $serveur = $em->getRepository('CaisseBundle:Serveur')->find($id);
        else
            $serveur = new Serveur();
        
        $form = $this->createForm(new ServeurType(), $serveur);
        
        
        if ($request->isMethod('POST')) {
            $form->bind($request);
            if ($form->isValid()) {
                $serveur = $form->getData();
                
                
                $repository = $em->getRepository('CaisseBundle:Serveur');
                $em->persist($serveur);
                $em->flush();
                $serveurs = $repository->findAll();
                 return $this->render('CaisseBundle:Serveur:listServeur.html.twig', array('serveurs' => $serveurs));
            }
        }
        return $this->render('CaisseBundle:Serveur:ajoutServeur.html.twig', array('form' => $form->createView(), 'id' => $id));

  
        
    }
    public function modifierServeurAction() {
        $em = $this->getDoctrine()->getManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        $serveur = $em->getRepository('CaisseBundle:Serveur')->find($id);
        $form = $this->createForm(new ServeurType(),$serveur);
        print "xxcxcxc";
        if ($request->isMethod('post')) {
            $serveur->bind($request);
            if ($form->isValid()) {
                $serveur = $form->getData();
                $em->persist($serveur);
                $em->flush();
                $url = $this->generateUrl("caisse_list_serveur");
                return $this->redirect($url);
            }
        }
        return $this->render('CaisseBundle:Serveur:ajoutServeur.html.twig', array('form' => $form->createView(), 'id' => $id));

    }
    
    public function supprimerServeurAction() {
        $request = $this->getRequest();
        $id = $request->get('id');
        $em = $this->getDoctrine()->getManager();
        $produit = $em->getRepository('CaisseBundle:Serveur')->find($id);
        $em->remove($produit);
        $em->flush();
        $url = $this->generateUrl("caisse_list_serveur");
        return $this->redirect($url);
    }
    public function  loginServeurAction(){
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getManager();
        
        $username = $this->getRequest()->get('username');
        $password = $this->getRequest()->get('password');
        
        
    }

}
